<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" v-if="admin_btn">
      <el-form-item label="检查场站" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入检查场站"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查月份" prop="month">
        <el-date-picker
          v-model="queryParams.checkMonth"
          type="month"
          size="small"
          value-format="yyyy-MM"
          placeholder="请选择检查月份">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['ledger:inspect:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['ledger:inspect:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
    </el-row>
    <div class="header" v-if="station_btn">
      <p style="margin-left: 5%">1、<a style="color: blue" href="/profile/files/叉车检查项目表.docx">点击下载</a>《叉车检查项目表》</p>
      <p style="margin-left: 5%">2、根据表格内容填写盖章后，按照页面提示上传</p>
      <p style="margin-left: 5%">3、准备核查表格填写内容是否正确，上传后将无法更改</p>
      <el-button
        type="primary"
        icon="el-icon-upload"
        size="mini"
        @click="handleAdd"
        v-hasPermi="['ledger:inspect:add']"
      >上传</el-button>
    </div>

    <el-table v-loading="loading" :data="inspectList" :max-height="tableHeight" @selection-change="handleSelectionChange">
      <el-table-column label="检查场站" align="left" prop="stationName" v-if="admin_btn" width="300"/>
      <el-table-column label="检查月份" align="center" prop="month" />
      <el-table-column label="检查者" align="center" prop="examiner" :formatter="typeFormat" />
      <el-table-column label="上传日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ledger:inspect:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ledger:inspect:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改叉车检查项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="选择检查月份" prop="month">
<!--          <el-input v-model="form.month" placeholder="请输入检查月份" />-->
          <el-date-picker clearable size="small" style="width: 80%"
                          v-model="form.month"
                          type="month"
                          placeholder="选择检查月份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查者" prop="examiner">
          <el-select v-model="form.examiner" placeholder="请选择检查者" style="width: 80%">
            <el-option label="自检" value="1" />
            <el-option label="第三方检查" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="上传检查盖章复检图" prop="createBy">
          <el-upload
            :multiple="true"
            accept=".jpg, .png, .gif, .bmp, .jpeg"
            :headers="upload.headers"
            :action="upload.url"
            :file-list="form.imgList"
            list-type="picture-card"
            :limit="upload.limit"
            :before-upload="beforeImageUpload"
            :on-exceed="exceedFile"
            :on-success="handlePictureSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <!--  详情  -->
    <el-dialog :title="title" :visible.sync="openView" width="650px">
      <el-form ref="form" :model="form"  label-width="120px">
        <el-form-item label="检查场站:" v-if="admin_btn">{{form.stationName}}</el-form-item>
        <el-form-item label="检查月份:" >{{form.month}}</el-form-item>
        <el-form-item label="检查者:" >{{type(form.examiner)}}</el-form-item>
        <el-form-item label="检查盖章复检图:" class="img" >
          <ul>
            <li v-for="img in form.imgList" style="text-align: center;">
              <el-image :preview-src-list="srcList" :src="img.url" style="width: 100px;height:100px;"></el-image>
            </li>
          </ul>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspect, getInspect, delInspect, addInspect, updateInspect, exportInspect } from "@/api/ledger/inspect";
import { getToken } from "@/utils/auth";
export default {
  name: "Inspect",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 叉车检查项目表格数据
      inspectList: [],
      srcList:[],
      // 弹出层标题
      title: "",
      upload: {
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        //  文件个数
        limit: 5
      },
      // 是否显示弹出层
      open: false,
      openView: false,
      station_btn: false,
      admin_btn: true,
      tableHeight: window.innerHeight - 275,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: undefined,
        stationName: undefined,
        month: undefined,
        checkMonth: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        month: [
          { required: true, message: "检查月份不能为空", trigger: "blur" }
        ],
        examiner: [
          { required: true, message: "检查者不能为空", trigger: "blur" }
        ],
      },
      dialogImageUrl: '',
      dialogVisible: false,
    };
  },
  created() {
    const stationId = this.$route.params && this.$route.params.stationId;
    this.queryParams.stationId=stationId;
    const stationName = this.$route.params && this.$route.params.stationName;
    this.queryParams.stationName=stationName;
    this.getList();

    const roles = this.$store.getters && this.$store.getters.roles;
    for (let rol of roles){
      if ('station'==rol  || 'hs_station'==rol){
        this.station_btn=true;
        this.admin_btn=false;
      }
    }
  },
  methods: {
    /** 查询叉车检查项目列表 */
    getList() {
      this.loading = true;
      listInspect(this.queryParams).then(response => {
        this.inspectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        stationId: undefined,
        month: undefined,
        examiner: undefined,
        delFlag: undefined,
        createTime: undefined,
        createId: undefined,
        updateTime: undefined,
        updateId: undefined,
        remark: undefined,
        imgList: [],
        imgs:[],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.checkMonth='';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "上传叉车检查项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInspect(id).then(response => {
        this.form = response.data;
        const imgs = [];
        this.form.imgList.map(function (item) {
          imgs.push(item.url);
        });
        this.form.imgs = imgs;
        this.open = true;
        this.title = "修改叉车检查项目";
      });
    },
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getInspect(id).then(response => {
        this.id = row.id;
        this.form = response.data;
        const imgs = [];
        this.form.imgList.map(function (item) {
          imgs.push(item.url);
        });
        this.srcList = imgs;
        this.openView = true;
        this.title = "叉车检查项目详情";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.imgs.length ==0){
            this.msgError("请上传图片");
          }else {
            if (this.form.id != undefined) {
              updateInspect(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addInspect(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("上传成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        }
      });
    },
    // 检查者类型
    typeFormat(row, column) {
      if (row.examiner == '1')
        return  '自检'
      if (row.examiner == '2')
        return  '第三方检查'
    },
    type(val) {
      if (val == '1')
        return  '自检'
      if (val == '2')
        return  '第三方检查'
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删该条除叉车检查项目的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInspect(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有叉车检查项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInspect(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    beforeImageUpload(file){
      const isLt2M = file.size / 1024 / 1024 < 10
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 10MB!')
        return false
      }
    },
    exceedFile(files, fileList) {
      this.$notify.warning({
        title: '警告',
        message: `最多只能上传${this.upload.limit}个图片`
      });
    },
    handlePictureSuccess(response, file) {
      this.form.imgs.push(response.fileName);
    },
    //图片上传
    handleRemove(file, fileList) {
      this.form.imgs.splice(this.form.imgs.indexOf(file.url),1);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    }
  }
};
</script>
<style>
  .img ul{
    padding: 0;
  }
  .img ul li{
      float:left;
      width:20%;
      list-style:none;
      width: 100px;
      margin-right: 2%;
    }
</style>
